﻿@using CNKI.TPI.Web.Admin.Model;
@{
    Layout = null;
}
@using CNKI.TPI.Web.Base
<script>
    //用户单位全选
    function companycheckall(e) {
        if ($(e).attr("checked") == "checked") {
            $(".checkboxcompany input[type='checkbox']").each(function () {
                $(this).attr("checked", "checked");
            })
        }
        else {
            $(".checkboxcompany input[type='checkbox']").each(function () {
                $(this).removeAttr("checked", "checked");
            })
        }
    }
    $(".checkboxcompany input[type='checkbox']").click(function () {
        //循环判断列表中的checkbox，如果全部选中则全选框呈勾选状态
        var m = 0;
        if ($(this).attr("checked") != "checked") {
            $("input.companyall").removeAttr("checked", "checked");
        }
        else {
            $(".checkboxcompany input[type='checkbox']").each(function () {
                if ($(this).attr("checked") != "checked") {
                    m = 1;
                }
            })
            if (m == 1) {
                $("input.companyall").removeAttr("checked", "checked");
            }
            else {
                $("input.companyall").attr("checked", "checked");
            }
        }

    })
    //删除用户单位
    function delUnitlist() {
        var ckbs = $(".checkboxcompany input[type='checkbox']:checked");
        if (ckbs.size() == 0) {
            messageAlert("请选择要删除的记录！");
            return;
        }
        confirmDialog('确定要删除吗?', function () {
            var checkedstr = [];
            ckbs.each(function () {
                checkedstr.push($(this).val());
            })
            var delurl = "@Html.Raw(Url.Action("DelUnit", "Admin_User"))";
            var data = { "idlist": checkedstr.toString() };
            dealdata(delurl, data);
        })
    }
    function delUnit(sysid) {
        var delurl = "@Html.Raw(Url.Action("DelUnit", "Admin_User"))";
        var data = { "idlist": sysid };
        confirmDialog('确定要删除吗?', function () {
            dealdata(delurl, data);
        })
    }
    //调用controll
    function dealdata(urlstr, datastr) {
        $.ajax({
            type: "POST",
            url: urlstr,
            data: datastr,
            success: function (m) {
                if (m == "True") {
                    messageAlert("操作成功！");
                    ajaxLoad("initload", '@Html.Raw(Url.Action("UnitIndex", "Admin_User"))');
                }
                else {
                    messageAlert("操作失败！");
                }
            },
            error: function (e)
            { }
        })
    }
    function saveUnit() {
        //验证
        var name = $.trim($("#unitname").val());
        if (name == "") {
            messageAlert("请输入单位名称！");
            return;
        }
        else {
            if (!regtext(name)) {
                messageAlert("请输入单位名称(不允许输入特殊字符)！");
                return;
            }
        }
        var con = $("#contract").val();
        if (con != "" && !regtext(con)) {
            messageAlert("请输入联系人(不允许输入特殊字符)！");
            return;
        }

        var tel = $("#tel").val();
        if (tel != "" && !regtel(tel)) {
            messageAlert("请输入正确的电话号码！");
            return;
        }
        var fax = $("#fax").val();
        if (fax != "" && !regtext(fax)) {
            messageAlert("请输入传真（不允许包含特殊字符）！");
            return;
        }
        var email = $("#email").val();
        if (email != "" && !regemail(email)) {
            messageAlert("请输入正确的E-Mail！");
            return;
        }
        var postcode = $("#postcode").val();
        if (postcode != "" && !regpostcode(postcode)) {
            messageAlert("请输入正确的邮政编码！");
            return;
        }
        var address = $("#address").val();
        if (address != "" && !regtext(address)) {
            messageAlert("请输入地址（不允许包含特殊字符）！");
            return;
        }
        var uniturl = $("#uniturl").val();
        if (uniturl != "" && !regurl(uniturl)) {
            messageAlert("请输入正确的网址（http://....）！");
            return;
        }
        var note = $("#note").val();
        if (note != "" && !regtext(note)) {
            messageAlert("请输入备注（不允许包含特殊字符）！");
            return;
        }
        var sysid = $("#FlageID").val();
        //判断单位名称是否重复
        $.ajax({
            type: "POST",
            async: false,
            url: "@Html.Raw(Url.Action("IsExitUnit", "Admin_User"))",
            data: { "name": name, "sysid": sysid },
            success: function (obj) {
                if (obj == 1) {
                    messageAlert("该单位名称已经存在！");
                }
                else {
                    savedata();
                }
            },
            error: function (e)
            { }
        })
    }
    function savedata() {
        var unitobj = {
            "SysID": $("#FlageID").val(),
            "UNITNAME": $("#unitname").val(),
            "UNITCODE": $("#unitcode").val(),
            "CONTRACT": $("#contract").val(),
            "TELEPHONE": $("#tel").val(),
            "FAX": $("#fax").val(),
            "EMAIL": $("#email").val(),
            "POSTCODE": $("#postcode").val(),
            "ADDRESS": $("#address").val(),
            "UNITURL": $("#uniturl").val(),
            "UNITTYPE": "0",
            "NOTES": $("#note").val()
        } //用户
        var dataold = { "unitobj": unitobj };
        var urlaction = "@Html.Raw(Url.Action("SaveUnitInfo", "Admin_User"))";
        $.ajax({
            type: "POST",
            url: urlaction,
            contentType: "application/json",
            data: JSON.stringify(dataold),
            success: function (msg) {
                if (msg == "True") {
                    messageAlert("操作成功！");
                    closeUnit();
                    ajaxLoad("initload", '@Html.Raw(Url.Action("UnitIndex", "Admin_User"))');
                }
                else {
                    messageAlert("操作失败！");
                }
            },
            error: function (e)
            { }
        })

    }
    function closeUnit() {
        $(".talkDivBg").hide();
        $(".unitform").hide();
    }
    function showUnit() {
        $(".talkDivBg").show();
        $(".unitform").show();
    }
    //添加用户单位
    function addUnit() {
        $("#FlageID").val(0);
        $("#unitname").val("");
        $("#unitcode").val("");
        $("#contract").val("");
        $("#tel").val("");
        $("#fax").val("");
        $("#email").val("");
        $("#postcode").val("");

        $("#address").val("");
        $("#uniturl").val("");
        $("#note").val("");

        $(".talkDiv-title span").html("新建用户单位");
        showUnit();
    }
    //编辑
    function modifyUnit(id, name, code, contract, tel, fax, email, post, address, url, note) {
        $(".talkDiv-title span").html("编辑用户单位");

        $("#FlageID").val(id);
        $("#unitname").val(name);
        $("#unitcode").val(code);
        $("#contract").val(contract);
        $("#tel").val(tel);
        $("#fax").val(fax);
        $("#email").val(email);
        $("#postcode").val(post);

        $("#address").val(address);
        $("#uniturl").val(url);
        $("#note").val(note);

        showUnit();
    }
</script>
<style>
   
</style>
<table width="100%" cellspacing="0" cellpadding="0" border="0" class="table table-grey">
    <thead>
        <tr class="thead">
            <th class="tdnum">
                <input type="checkbox" class="companyall" value="" name="" onclick="companycheckall(this)">序号</th>
            <th class=''>单位名称</th>
            <th class=''>联系人</th>
            <th class=''>电话</th>
            <th class=''>地址</th>
            <th>操作</th>
        </tr>
    </thead>
    <tbody>
        @{if (ViewBag.CompanyList != null)
          {
              int sortno = ViewBag.PageSize * (ViewBag.PageNow - 1) + 1;
              foreach (SYS_CMS_USER_UNIT item in ViewBag.CompanyList)
              {
                  string str = "even";
                  if (sortno % 2 == 0)
                  {
                      str = "odd";
                  }
            <tr class='@str'>
                <td class="checkboxcompany">
                    <input type="checkbox" value="@item.SYSID"  >@sortno</td>
                <td><a class="atitle" title="@ColumnAliasHelper.GetContentNoRed(item.UNITNAME)">@Html.Raw(ColumnAliasHelper.SubContent(item.UNITNAME, 10))</a></td>
                <td><a  class="atitle" title="@item.CONTRACT">@Html.Raw(ColumnAliasHelper.SubContent(item.CONTRACT, 10))</a></td>
                <td><a class="atitle" title="@item.TELEPHONE">@Html.Raw(ColumnAliasHelper.SubContent(item.TELEPHONE, 15))</a></td>
                <td><a class="atitle" title="@item.ADDRESS">@Html.Raw(ColumnAliasHelper.SubContent(item.ADDRESS, 20))</a></td>
                <td class="tc">
                    <a onclick="ajaxLoad('initload','@Html.Raw(Url.Action("DeptManager", "Admin_User"))?unitid=@item.SYSID')"  href="javascript:void(0)">部门管理</a>
                    <a onclick="modifyUnit('@item.SYSID','@item.UNITNAME','@item.UNITCODE','@item.CONTRACT','@item.TELEPHONE','@item.FAX','@item.EMAIL','@item.POSTCODE','@item.ADDRESS','@item.UNITURL','@item.NOTES')" href="javascript:void(0)">编辑</a>
                    <a onclick="delUnit('@item.SYSID')" href="javascript:void(0)">删除</a>

                </td>
            </tr>
                  sortno = sortno + 1;
              }
          }

        }
    </tbody>
</table>
@*//分页*@
<div class="pagenav pagenav-02">
    @*上一页*@
    @if (ViewBag.TotalPage > 0)
    {
        if (ViewBag.PageNow != 1)
        {
            int thispage = ViewBag.PageNow - 1;
        <a href="javascript:void(0);" class="page-prev" onclick="searchPageNew('@thispage')" >上一页</a>
        }
        else
        {
        <a href="javascript:return false;"  onclick="return false" class="page-prev disable">上一页</a>
        }
@*循环初始化页码*@
        foreach (string numpage in ViewBag.PageList)
        {
            if (numpage != "0")
            {
                int m = int.Parse(numpage);
                if (m == ViewBag.PageNow)
                {
        <a href="javascript:void(0);" class="active" onclick="searchPageNew('@numpage')">@numpage</a>
                    
                }
                else
                {
        <a href="javascript:void(0);" onclick="searchPageNew('@numpage')">@numpage</a>
                }

            }
            else
            {
        <i class="iconfont icon-dian2"></i>
            }

        }
@*下一页*@
        if (ViewBag.TotalPage > 0 && ViewBag.PageNow != ViewBag.TotalPage)
        {
            int nextpage = ViewBag.PageNow + 1;
        <a href="javascript:void(0);" class="page-next" onclick="searchPageNew('@nextpage')" >下一页</a>
        }
        else
        {
        <a href="javascript:return false;"  onclick="return false" class="page-prev disable">下一页</a>
        }
    }
    @if (@ViewBag.Count == 0)
    {
        <label>暂无数据！</label>
        
    }
    else
    {
          
        <label>共 </label>
        <label>@ViewBag.Count </label>
        <label>条数据 </label>
    }
</div>

<div class='posr' style="padding-bottom: 20px">
    <a class="btn btn-m btn-submit btn-defa-f" href="javascript:void(0);" onclick="addUnit()">添加</a>
    <a class="btn btn-m btn-submit btn-defa-f" href="javascript:void(0);" onclick="delUnitlist()">删除</a>
</div>
<!--透明背景-->
<div class="talkDivBg" style="display: none; z-index: 999">

    <!--talkDiv弹出层(不带图片，宽550)-->
    <div class="talkDiv talkDivTr talkW-550 form-h unitform" style='margin-top: -278px; display: none'>
        <div class="talkDiv-title">
            <span>新建用户单位</span>
            <a class="icon icon-close" title="关闭" href="javascript:void(0);" onclick="closeUnit()"><i class="iconfont icon-error3"></i></a>
        </div>
        <div class="talkDiv-body talkDiv-body1">

            <div class="f-gp">
                <div class="f-lb">
                    <label class="f-label">单位名称：</label>
                </div>
                <div class="f-ct">
                    <input type="text" value="" class="f-text span3" id="unitname" maxlength="127">
                    <span class="f-label-re">*</span>
                </div>
            </div>

            <div class="f-gp">
                <div class="f-lb">
                    <label class="f-label">联系人：</label>
                </div>
                <div class="f-ct">
                    <input type="text" value="" class="f-text span3" id="contract" maxlength="20">
                </div>
            </div>

            <div class="f-gp">
                <div class="f-lb">
                    <label class="f-label">电话：</label>
                </div>
                <div class="f-ct">
                    <input type="text" value="" class="f-text span3" id="tel" maxlength="255">
                </div>
            </div>

            <div class="f-gp">
                <div class="f-lb">
                    <label class="f-label">传真：</label>
                </div>
                <div class="f-ct">
                    <input type="text" value="" class="f-text span3" id="fax" maxlength="255">
                </div>
            </div>

            <div class="f-gp">
                <div class="f-lb">
                    <label class="f-label">E-mail：</label>
                </div>
                <div class="f-ct">
                    <input type="text" value="" class="f-text span3" id="email" maxlength="255">
                </div>
            </div>

            <div class="f-gp">
                <div class="f-lb">
                    <label class="f-label">邮编：</label>
                </div>
                <div class="f-ct">
                    <input type="text" value="" class="f-text span3" id="postcode" maxlength="12">
                </div>
            </div>

            <div class="f-gp">
                <div class="f-lb">
                    <label class="f-label">地址：</label>
                </div>
                <div class="f-ct">
                    <input type="text" value="" class="f-text span3" id="address" maxlength="254">
                </div>
            </div>

            <div class="f-gp">
                <div class="f-lb">
                    <label class="f-label">单位网址：</label>
                </div>
                <div class="f-ct">
                    <textarea rows="4" class="f-text span3" id="uniturl" maxlength="254"></textarea>
                </div>
            </div>

            <div class="f-gp">
                <div class="f-lb">
                    <label class="f-label">备注：</label>
                </div>
                <div class="f-ct">
                    <textarea rows="4" class="f-textarea span5" id="note" maxlength="500"></textarea>
                </div>
            </div>

            <div class="f-gp">
                <div class="f-lb">
                    <label class="f-label"></label>
                </div>
                <div class="f-ct" style="margin-left: 150px">
                    <a href="javascript:void(0);" class="btn btn-m btn-prim-f btn-submit" onclick="saveUnit()">保存</a> &#12288;
                    <a href="javascript:void(0);" class="btn btn-m btn-submit btn-defa-f" onclick="closeUnit()">取消</a>
                </div>
            </div>
        </div>
    </div>
</div>